package com.slwish.xty.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.slwish.xty.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 创建人：陈浩
 * 创建时间：2017/9/20
 */

public class TimerUtils {
    private int time = 60;

    private Timer timer;
    private Context mContext;
    private TextView btnSure;

//    private String btnText;

    public TimerUtils(Context mContext, TextView btnSure) {
        super();
        this.mContext = mContext;
        this.btnSure = btnSure;
//        this.btnText = btnText;
    }


    public void RunTimer() {
        timer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                time--;
                Message msg = handler.obtainMessage();
                msg.what = 1;
                handler.sendMessage(msg);

            }
        };
        timer.schedule(task, 100, 1000);
    }


    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    if (time > 0) {
                        btnSure.setEnabled(false);
                        btnSure.setText(time + "s");
//                        btnSure.setTextSize(14);
                        btnSure.setTextColor(mContext.getResources().getColor(R.color.identify_verify_color));
                        btnSure.setBackgroundResource(R.drawable.verify_identify_text_shape); //设置按钮为灰色，这时是不能点击的

                    } else {

                        timer.cancel();
                        btnSure.setText("重新发送");
                        btnSure.setEnabled(true);
//                        btnSure.setTextSize(14);
                        btnSure.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
                        btnSure.setBackgroundResource(R.drawable.verify_text_shape);  //还原背景色
                    }

                    break;


                default:
                    break;
            }

        }

    };

}
